/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO_BUS;

import java.util.Vector;
import org.w3c.dom.Document;

/**
 *
 * @author MinhNhat
 */
public class InsertSentence extends MySentence {

    String Table;
    Vector Columns;
    Vector Values;

    public InsertSentence(String strInsert) {
        Values = new Vector();
        Columns = new Vector();
        strInsert = strInsert.trim();
        strInsert = strInsert.replaceAll("(\r\n|\n)", " ");
        if (!strInsert.isEmpty()) {
            strInsert = GetTableName(strInsert);
        }

        if (!strInsert.isEmpty()) {
            strInsert = GetColumns(strInsert);
        }

        if (!strInsert.isEmpty()) {
            GetValues(strInsert);
        }

    }

    public String GetTableName(String str) {
        String s1 = str.toLowerCase();
        String s2 = "into";
        int indexOfTableName = s1.indexOf(s2);
        String core = str.substring(indexOfTableName + s2.length(), str.length()).trim();

        s1 = core.toLowerCase();

        int indexOfEndTableName = -1;
        boolean flag = false;

        int indexOfMoNgoac, indexOfDongNgoac, indexOfValue;

        indexOfMoNgoac = s1.indexOf("(");
        indexOfDongNgoac = s1.indexOf(")");
        indexOfValue = s1.indexOf("values ");

        if (indexOfMoNgoac < indexOfDongNgoac && indexOfDongNgoac < indexOfValue) {
            Table = core.substring(0, indexOfMoNgoac).trim();
            return core.substring(indexOfMoNgoac, core.length()).trim();
        }

        if (indexOfValue < indexOfMoNgoac && indexOfMoNgoac < indexOfDongNgoac) {
            Table = core.substring(0, indexOfValue).trim();
            return core.substring(indexOfValue, core.length()).trim();
        }
        return "";
    }

    public String GetColumns(String str) {
        str = str.trim();
        Columns = new Vector();
        if (str.isEmpty()) {
            return str;
        }

        if (str.charAt(0) != '(') {
            return str;
        }

        String s1 = str.toLowerCase();
        int indexOfEndColumn = s1.indexOf(" values ");

        if (indexOfEndColumn != -1) {
            indexOfEndColumn = str.indexOf(")");
            if (indexOfEndColumn == -1) {
                return "";
            }
            String temp = str.substring(1, indexOfEndColumn);
            String[] columns = temp.split(",");
            for (int i = 0; i < columns.length; i++) {
                Columns.add(columns[i].trim());
            }

            str = str.substring(indexOfEndColumn + 1, str.length()).trim();
        }

        return str;
    }

    public String GetValues(String str) {
        Values = new Vector();
        int indexOfColumns = str.indexOf("(");
        int indexOfEndColumns = str.indexOf(")");

        if (indexOfColumns == -1 || indexOfEndColumns == -1) {
            return str;
        }
        str = str.substring(indexOfColumns + 1, indexOfEndColumns);
        String[] values = str.split(",");
        for (int i = 0; i < values.length; i++) {
            Values.add(values[i].replace("'", "").trim());
        }
        return str;
    }

    public static boolean isInsertSentence(String str) {
        String s1 = str.toLowerCase().trim();
        String s2 = "insert ";
        int indexOfString = s1.indexOf(s2);
        if (indexOfString == 0) {
            return true;
        }
        return false;
    }

    public boolean Excute(Document doc, Vector Tables) {
        try {
            if (Values.size() == 0) {
                return false;
            }

            for (int i = 0; i < Tables.size(); i++) {
                MyTable table = (MyTable) Tables.get(i);
                String tableName = table.GetName();
                if (tableName.compareTo(Table) == 0) {
                    if (table.GetHeader().size() != Columns.size() && Columns.size() != 0) {
                        return false;
                    }

                    if (!table.isRightColumns(Columns)) {
                        return false;
                    }

                    return table.InsertData(doc, Columns, Values);
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return false;
        }
        return false;
    }
}
